package com.fz.controller;


import com.fz.entity.Admin;
import com.fz.mapper.AdminLoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by Z-C-Y on 2018-03-07.
 * 后台登录注册
 */
@Controller
//@ResponseBody
public class AdminLoginController {
    @Autowired
    protected AdminLoginMapper adminLoginMapper;

    @RequestMapping(value = "/admin/hlogin")
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*得到用用户提交的登录信息*/
        String username = null;
        String password = null;
        String flag = null;

        if (request.getParameter("username") != null && !"".equals(request.getParameter("username"))) {
            username = request.getParameter("username");
        }
        if (request.getParameter("password") != null && !"".equals(request.getParameter("password"))) {
            password = request.getParameter("password");
        }
        if (username != null && password != null) {
            List<Admin> listAdmin = adminLoginMapper.queryAdmin(username, password);
            for (Admin admin : listAdmin) {
                if (!username.equals(admin.getUsername())) {
                    /*说明没有此用户*/
                } else if (!password.equals(admin.getPassword())) {
                    /*说明输入的密码不对*/
                } else {
                    // request.getRequestDispatcher("/admin/hindex.jsp").forward(request,response);
                    HttpSession seesion = request.getSession();
                    seesion.setAttribute("adminUser", username);
                    flag = "forward:/hindex.jsp";
                }
            }
        }
        return flag;
    }

    /*用户注册*/
    @RequestMapping(value = "/admin/register")
    public String register(HttpServletRequest request) {
        String username = null;
        String password = null;
        String password2 = null;
        if ((request.getParameter("username") != null) && (!"".equals(request.getParameter("username")))) {
            username = request.getParameter("username");
        }
        if ((request.getParameter("password") != null) && (!"".equals(request.getParameter("password")))) {
            password = request.getParameter("password");
        }
        if ((request.getParameter("password2") != null) && (!"".equals(request.getParameter("password2")))) {
            password2 = request.getParameter("password2");
        }

        if (password.equals(password2)) {
            int a = adminLoginMapper.addAegister(username, password);
            if (a <= 1) {
                /*注册成功*/
                System.out.println(a);
            } else {
                /*注册失败！！！*/
            }

        }


        return "/admin/hlogin";
    }

}
